package com.lzw.service.impl.admin;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.lzw.mapper.admin.MaintenanceWorkerManagementMapper;
import com.lzw.pojo.MaintenanceWorker;
import com.lzw.service.admin.MaintenanceWorkerManagementService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class MaintenanceWorkerManagementServiceImpl implements MaintenanceWorkerManagementService {

    @Autowired
    private MaintenanceWorkerManagementMapper maintenanceWorkerManagementMapper;

    @Override
    public List<MaintenanceWorker> listMaintenanceWorker(Integer pageNum, Integer pageSize, String name, String phone) {
        PageHelper.startPage(pageNum,pageSize);
        List<MaintenanceWorker> list = maintenanceWorkerManagementMapper.listMaintenanceWorker(name, phone);
        Page<MaintenanceWorker> maintenanceWorkers = (Page<MaintenanceWorker>) list;
        return maintenanceWorkers;
    }

    @Override
    public void addMaintenanceWorker(MaintenanceWorker maintenanceWorker) {
        //检查维修员是否已存在
        if (maintenanceWorkerManagementMapper.countByName(maintenanceWorker.getName()) > 0){
            throw new IllegalArgumentException("维修员姓名已存在");
        }

        //检查维修员用户名是否已存在
        if (maintenanceWorkerManagementMapper.countByUsername(maintenanceWorker.getUsername()) > 0){
            throw new IllegalArgumentException("维修员用户名已存在");
        }

        //检查维修员电话是否存在
        if (maintenanceWorkerManagementMapper.countByPhone(maintenanceWorker.getPhone()) > 0){
            throw new IllegalArgumentException("维修员联系电话已存在");
        }

        maintenanceWorkerManagementMapper.addMaintenanceWorker(maintenanceWorker);
    }

    @Override
    public void deleteMaintenanceWorker(List<Integer> ids) {
        maintenanceWorkerManagementMapper.deleteMaintenanceWorker(ids);
    }

    @Override
    public void updateMaintenanceWorker(MaintenanceWorker maintenanceWorker) {
        MaintenanceWorker existing = maintenanceWorkerManagementMapper.getMaintenanceWorkerById(maintenanceWorker.getWorkerId());

        //检查维修员是否存在
        if (existing == null){
            throw new IllegalArgumentException("维修员不存在");
        }

        //判断维修员姓名是否重复
        if (!existing.getName().equals(maintenanceWorker.getName())
                && maintenanceWorkerManagementMapper.countByName(maintenanceWorker.getName()) > 0){
            throw new IllegalArgumentException("维修员名字已存在");
        }

        //判断维序员用户名是否重复
        if (!existing.getUsername().equals(maintenanceWorker.getUsername())
                && maintenanceWorkerManagementMapper.countByUsername(maintenanceWorker.getUsername()) > 0){
            throw new IllegalArgumentException("维修员用户名已存在");
        }

        //判断维修员电话是否重复
        if (!existing.getPhone().equals(maintenanceWorker.getPhone())
                && maintenanceWorkerManagementMapper.countByPhone(maintenanceWorker.getPhone()) > 0){
            throw new IllegalArgumentException("维修员电话已存在");
        }

        maintenanceWorkerManagementMapper.updateMaintenanceWorker(maintenanceWorker);
    }

    @Override
    public MaintenanceWorker getMaintenanceWorkerById(int id) {
        return maintenanceWorkerManagementMapper.getMaintenanceWorkerById(id);
    }
}
